﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework.Constraints;
using NUnit.Framework;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using SAKS.ViewModels;
using SAKS.Models;
using SAKS.Services;
using SAKS.IServices;

namespace SAKS.UnitTest.ServicesTest
{
    class DocumentServicesTest : TestBase
    {
        IDocumentServices DocumentServices { get; set; }
        public DocumentServicesTest() {
            DocumentServices = new DocumentServices();
        }

        [Test]
        public void DontLoadDocFileTest()
        {
            DocumentInfo info;
            using (var session = sessionFactory.OpenSession())
            {
                info = new DocumentInfo()
                {
                    SerialNumber = 123,
                    Name = "",
                    SecurityLevel = SecurityLevel.Free
                };
                DocumentServices.Session = session;
                using (var tx = session.BeginTransaction())
                {
                    DocumentServices.SaveInfo(info);
                    tx.Commit();
                }
            }
            using (var session = sessionFactory.OpenSession())
            {

                DocumentServices.Session = session;
                var info1 = DocumentServices.GetInfo(info.Id);
                Assert.IsNull(info.DocFile);
            }
        }



        [Test]
        public void DontCascadeSaveDocFileTest()
        {
            DocFile docfile;
            DocumentInfo docinfo;
            Guid fileId;
            docfile = new DocFile()
            {
                Doc = new byte[10],
                Name = ""
            };
            docinfo = new DocumentInfo()
            {
                SerialNumber = 123,
                Name = "",
                SecurityLevel = SecurityLevel.Free
            };
            using (var session = sessionFactory.OpenSession())
            {
                DocumentServices.Session = session;
                docfile.DocumentInfo = docinfo;
                fileId = (Guid)session.Save(docfile);
                session.Flush();
                Assert.IsTrue(docinfo.Id == default(Guid));//docinfo没有被级联保存

                docinfo.FileId = fileId;
                docinfo.DocFile = docfile;
                session.SaveOrUpdate(docinfo);
                session.Flush();//没有级联保存docfile

            }
            
        }

        private void CreateDate(out Guid? docfileId, out Guid? docinfoId)
        {
            var docfile = new DocFile()
            {
                Doc = new byte[10],
                Name = ""
            };
            var docinfo = new DocumentInfo()
            {
                SerialNumber = 123,
                Name = "",
                SecurityLevel = SecurityLevel.Free
            };
            using (var session = sessionFactory.OpenSession())
            {
                DocumentServices.Session = session;
                docfile.DocumentInfo = docinfo;
                docfileId = (Guid)session.Save(docfile);
                docinfo.FileId = docfileId;
                docinfo.DocFile = docfile;
                docinfoId = (Guid)session.Save(docinfo);
                session.Flush();
            }
        }
        [Test]
        public void CascadeDeleteDocFileTest()
        {
            Guid? docfileId;
            Guid? docinfoId;
            CreateDate(out docfileId, out docinfoId);

            using (var session = sessionFactory.OpenSession())
            {
                try
                {
                    var file = session.Get<DocFile>(docfileId);
                    session.Delete(file);//应该跑出异常，无法删除docFile
                    session.Flush();
                    Assert.IsTrue(false);
                }
                catch(Exception)
                {
                    Assert.IsTrue(true);
                }
            }
        }

        [Test]
        public void CascadeDeleteDocInfoTest()
        {
            Guid? docfileId;
            Guid? docinfoId;
            CreateDate(out docfileId, out docinfoId);
            using (var session = sessionFactory.OpenSession())
            {
                var info = session.Get<DocumentInfo>(docinfoId);
                session.Delete(info);
                session.Flush();

                Assert.IsTrue(session.Get<DocFile>(docfileId) == null);
                Assert.IsTrue(session.Get<DocumentInfo>(docinfoId) == null);//级联删除成功
            }
        }
    }
}
